package vn.bus.service;

import java.util.ArrayList;

import com.google.gson.Gson;

import java.util.*;

import vn.bus.models.ConnectionManager;
import vn.bus.dto.ConnectionDTO;

public class ConnectionService {

	public ArrayList<ConnectionDTO> getAllConns() {
		ArrayList<ConnectionDTO> allConns = null;
		try {
			ConnectionManager connManager = new ConnectionManager();
			allConns = connManager.getAllConnections();
		} catch (Exception e) {
			System.out.println("error");
		}
		return allConns;
	}

	public String allConnsJson() {
		Gson gson = new Gson();
		return gson.toJson(getAllConns());
	}

	public Hashtable<String, ArrayList<ConnectionDTO>> loadAllNodeConns() {

		Hashtable<String, ArrayList<ConnectionDTO>> NodeConns = 
				new Hashtable<String, ArrayList<ConnectionDTO>>();
		
		ArrayList<ConnectionDTO> allConns = getAllConns();
		
		ArrayList<ConnectionDTO> listConns;
		
		for (int i = 0; i < allConns.size(); i++) {

			ConnectionDTO conns = new ConnectionDTO();
			conns = allConns.get(i);

			if (NodeConns.get(conns.getStation()) == null) {
				listConns = new ArrayList<ConnectionDTO>();
				listConns.add(conns);
				NodeConns.put(conns.getStation(), listConns);
			} else {
				listConns = new ArrayList<ConnectionDTO>();
				listConns = NodeConns.get(conns.getStation());
				listConns.add(conns);
				NodeConns.put(conns.getStation(), listConns);
			}
		}
		return NodeConns;
	}
	public String updatePolyline(String polyline, String station, String next){
		String rs = "";
		try {
			ConnectionManager updatePoly = new ConnectionManager();
			rs = updatePoly.updatePolyline(polyline, station, next);
		} catch (Exception e) {
			System.out.println("error");
		}
		return rs;
	}
}
